<template>
    <c-dialog v-model:show="dialogFlag" v-if="dialogFlag" :title="title" @confirm="selected" @close="close" :btnDisabled="type==3">
        <div class="">
            <div class="addBox">
                <div class="label">公司名称</div>
                <el-input v-model="companyName" placeholder="输入营业执照上的公司名称" clearable />
                <div style="padding: 0 0 0 10px;">
                    <el-button type="success" plain @click="add">添加</el-button>
                </div>
            </div>
            <c-table ref="tableRef" :page="{ pageSize:limit, pageIndex:page, total }" :lists="tableData" @current-change="handleCurrentChange" :highlightCurrentRow="true" :sequence="false" className="small-el-table"
        @size-change="handleSizeChange" :loading="loading" rowKey="companyId" @table-current-change="tableCurrentChange">
                <el-table-column label="公司名称" min-width="150" prop="companyName" />
                <el-table-column label="操作" width="70px">
                    <template #default="scope">
                        <el-button type="text" icon="delete" size="small" @click="del(scope.row)">删除</el-button>
                    </template>
                  </el-table-column>
            </c-table>
        </div>
    </c-dialog>
</template>
<script setup>
import { ref,onMounted } from 'vue';
import {getCompanyList,addCompany,delCompany} from '@/api/system'
import cDialog from '@/components/custom/cDialog.vue'
import { message } from '@/utils/element'
import { ElMessageBox } from 'element-plus'

import cTable from '@/components/custom/cTable.vue'
//import {encodeRSA} from '@/utils/encryp'


const props = defineProps({
    show: {
        type: Boolean,
        default: false
    },
    title: String,
    editData: Object,
    type:Number // 操作类型：1为新增，2为编辑，3为查看
})
const emit = defineEmits(['update:show','success'])

const formData = ref({})
const page = ref(1)
const limit = ref(10)
const total = ref(0)
const tableData = ref([])
const loading = ref(true)
const tableRef = ref(null)
const companyName = ref(null)
const currentItem = ref({})


onMounted(()=>{
    getTableData()
})

const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}
// 分页
const handleSizeChange = (val) => {
  page.value = 1
  limit.value = val
  getTableData()
}

const tableCurrentChange = (item)=>{
    //console.log("选择表格数据",item)
    currentItem.value = item
}

const userForm = ref(null)

const dialogFlag = ref(props.show)

const add = async() => {
    if(!companyName.value){
        return message('请输入公司名称',3)
    }
    const res = await addCompany({companyName:companyName.value})
    if(res.code == 0){
        //message('数据提交成功',2)
        //emit('success')
        //close()
        page.value = 1
        companyName.value = ''
        getTableData()
    }else{
        message(res.message,3) 
    }
}

const getTableData = async ()=>{
    const res = await getCompanyList({page: page.value, limit: limit.value})
    if(res.code ==0 ){
        tableData.value = res.data
        total.value = res.total
    }
    loading.value = false
}

const del = (item)=>{
    ElMessageBox.confirm('确认删除吗?','提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(async () => {
        const res = await delCompany([item.companyId])
        if(res.code ==0 ){
            page.value = 1
            getTableData()
        }
    }).catch(() => {
       // message('删除失败',3) 
    })
}

const selected = ()=>{
    emit('success',currentItem.value.companyId)
    close()
}
const close = () => {
    emit('update:show', false)
}
</script>
<style lang="scss" scoped>
    .addBox{
        display: flex;
        height: 40px;
        line-height: 30px;
        .label{
            width: 70px;
        }
    }
</style>
